﻿//-----------------------------------------------------------------------
// <copyright file="ClientAuthorizationService.cs" company="Alan Rodrigues">
//     Copyright (c) Alan Rodrigues. All rights reserved.
// </copyright>
// <developed>Alan Rodrigues (operninha@gmail.com)</developed>
//-----------------------------------------------------------------------

namespace Authorization
{
    using System;
    using System.ServiceModel;

    /// <summary>
    /// 
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple, Namespace="http://authorizationservices/v1")]
    public class ClientAuthorizationService :  IClientAuthorizationService
    {
        #region IUserAuthorizationService Members

        #region OperationCollection

        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationName"></param>
        /// <param name="scopeName"></param>
        /// <returns></returns>
        public OperationCollection GetOperationsForUser(string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
             return UserAuthorizarionServiceBase.GetOperationsForUser(null, applicationName, scopeName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationName"></param>
        /// <param name="scopeName"></param>
        /// <returns></returns>
        public string[] GetOperationNamesForUser(string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            return UserAuthorizarionServiceBase.GetOperationNamesForUser(null, applicationName, scopeName);
        }
        #endregion

        #region TaskCollection
        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationName"></param>
        /// <param name="scopeName"></param>
        /// <returns></returns>
        public TaskCollection GetTasksForUser(string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            return UserAuthorizarionServiceBase.GetTasksForUser(null, applicationName, scopeName);
        }

        public string[] GetTaskNamesForUser(string userName, string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            return UserAuthorizarionServiceBase.GetTaskNamesForUser(null, applicationName, scopeName);
        }

        public bool IsUserInTask(string userName, string applicationName, string taskName, string[] scopeNames)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref taskName, true, true, false, 64, "roleName");
            return UserAuthorizarionServiceBase.IsUserInTask(null, applicationName, taskName, scopeNames);
        }
        #endregion

        #region RoleCollection
        public RoleAssignmentCollection GetRoleAssignmentsForUser(string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            return UserAuthorizarionServiceBase.GetRoleAssignmentsForUser(null, applicationName, scopeName);            
        }
        
        public bool IsUserInRoleAssignment(string applicationName, string scopeName, string roleName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref roleName, true, true, false, 64, "roleName");
            return UserAuthorizarionServiceBase.IsUserInRoleAssignment(null, applicationName, scopeName, roleName);
        }

        public string[] GetRoleAssignmentNamesForUser(string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            return UserAuthorizarionServiceBase.GetRoleAssignmentNamesForUser(null, applicationName, scopeName);
        }
        #endregion

        #region Scope
        public string[] GetAllAssignedScopesForUser(string applicationName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            return UserAuthorizarionServiceBase.GetAllAssignedScopesForUser(null, applicationName, null);
        }

        public string[] GetAllAssignedScopesForUser(string applicationName, string filterPattern)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            return UserAuthorizarionServiceBase.GetAllAssignedScopesForUser(null, applicationName, filterPattern);
        }

        public AuthorizationResponseWithCursor<string[]> GetAssignedScopesPageForUser(string applicationName, int pageSize, string cursor)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            string[] scopes = UserAuthorizarionServiceBase.GetAssignedScopesPageForUser(null, applicationName, pageSize, ref cursor);
            return new AuthorizationResponseWithCursor<string[]>(scopes, cursor);  
        }
        #endregion


        #region AccessCheck
        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationName"></param>
        /// <param name="operationId"></param>
        /// <param name="scopeName"></param>
        /// <returns></returns>
        public bool AccessCheck(string applicationName, int operationId, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            return UserAuthorizarionServiceBase.AccessCheck(null, applicationName, operationId, scopeName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationName"></param>
        /// <param name="operationName"></param>
        /// <param name="scopeName"></param>
        /// <returns></returns>
        public bool AccessCheck(string applicationName, string operationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref operationName, true, true, false, 64, "operationName");
            
            return UserAuthorizarionServiceBase.AccessCheck(null, applicationName, operationName, scopeName);
        }

        public int[] AccessCheck(string applicationName, int[] operationIds, string[] scopeNames)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            if (operationIds == null) throw new ArgumentNullException("operationIds");
           
            return UserAuthorizarionServiceBase.AccessCheck(null, applicationName, operationIds, scopeNames);
        }

        public OperationCollection AccessCheck(string applicationName, string[] operationNames, string[] scopeNames)
        {   
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckArrayParameter(ref operationNames, true, true, false, 64, "operationNames");

            return UserAuthorizarionServiceBase.AccessCheck(null, applicationName, operationNames, scopeNames);
        }

        public bool AccessCheck(string applicationName, int operationId, string scopeName, string[] variableNames, object[] variableValues)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");         

            //TODO: TERMINAR
            //return GetUserAuthorization(applicationName).AccessCheck( operationId, scopeName,);
            return false;
        }
        #endregion


       
        #endregion


        #region Private Methods
        //TODO: ADICIONAR NO WCF ServiceAuthorizationManager (AuthorizationContext para Servicos)
        //TODO: Verificar usuarios nao autenticados  
              
        #endregion

        #region IClientAuthorizationService Members


        

        #endregion
    }
}
